package cn.net.ecode.elec.api.web;

import cn.net.ecode.annotation.Login;
import cn.net.ecode.common.utils.IdGen;
import cn.net.ecode.common.utils.StringUtils;
import cn.net.ecode.common.web.BaseController;
import cn.net.ecode.constant.SubmitPlanDayEnum;
import cn.net.ecode.elec.plan.entity.ElecPlan;
import cn.net.ecode.elec.plan.service.ElecPlanService;
import cn.net.ecode.mdm.entity.MdmBtypeInfo;
import cn.net.ecode.mdm.service.MdmBtypeInfoService;
import cn.net.ecode.modules.sys.entity.User;
import cn.net.ecode.util.DateUtils;
import cn.net.ecode.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @ClassName AppElecPlanController
 * @Description 报电量接口
 * @Author AlanShelby
 * @Date 2019/8/28 0028 14:55
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/plan")
public class AppElecPlanController extends BaseController {

    @Autowired
    private ElecPlanService elecPlanService;

    @Autowired
    private MdmBtypeInfoService btypeInfoService;

    /**
     * @Method submitPlan
     * @Description 提报电量接口
     * @Author AlanShelby
     * @Date 14:58 2019/8/28 0028
     * @Param [elecPlan]
     * @Return cn.net.ecode.util.R
     */
    @Login
    @RequestMapping("/submitPlan")
    public R submitPlan(@RequestBody ElecPlan elecPlan) {
        // 检验参数
        StringBuilder message = new StringBuilder();
        if (!beanValidator(message, elecPlan)) {
            return R.error(message.toString());
        }

        if (elecPlan.getKwh() == null) {
            return R.error("计划用电量不能为空");
        }

        // 判断企业是否存在
        if (StringUtils.isNotBlank(elecPlan.getBtyteId())) {
            MdmBtypeInfo mdmBtypeInfo = btypeInfoService.get(elecPlan.getBtyteId());
            if (mdmBtypeInfo == null) {
                return R.error("企业信息有误，请重新登录");
            }
        } else {
            return R.error("企业信息有误，请重新登录");
        }


        // 校验时间
        Date now = new Date();
        Date nextMonth = cn.net.ecode.util.DateUtils.addDateMonths(now, 1);
        String yearAndMonth = cn.net.ecode.util.DateUtils.format(nextMonth, "yyyy-MM");
        Integer day = Integer.parseInt(DateUtils.format(now, "dd"));

        if (day < SubmitPlanDayEnum.BEGIN.getCode() || day > SubmitPlanDayEnum.END.getCode()) {
            return R.error("不在可提报计划电量范围之内");
        }

        // 检验次数
        elecPlan.setYearAndMonth(yearAndMonth);
        List<ElecPlan> planList = elecPlanService.findList(elecPlan);
        if (planList.size() > 0) {
            return R.error("该月份已提报过计划，无需重复提报");
        }

        // 保存数据
        User currentUser = new User(elecPlan.getUserId());
        elecPlan.setIsNewRecord(true);
        elecPlan.setId(IdGen.nextId());
        elecPlan.setCreateBy(currentUser);
        elecPlan.setUpdateBy(currentUser);
        elecPlan.setYearAndMonth(yearAndMonth);
        elecPlanService.save(elecPlan);

        return R.ok();
    }

    /**
     * @Method planList
     * @Description 获取某一企业报计划申报明细
     * @Author AlanShelby
     * @Date 15:16 2019/9/5 0005
     * @Param [btyteId]
     * @Return cn.net.ecode.util.R
     */
    @Login
    @RequestMapping("/planList")
    public R planList(@RequestParam String btyteId) {
        ElecPlan elecPlan = new ElecPlan();
        elecPlan.setBtyteId(btyteId);
        List<ElecPlan> planList = elecPlanService.findList(elecPlan);

        return R.ok().put("planList", planList);
    }
}
